package com.example.onlinemarket;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.widget.ImageView;

import androidx.core.app.ActivityCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.onlinemarket.model.User;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import static com.example.onlinemarket.util.HttpUtil.downloadFile;

import okhttp3.OkHttpClient;
import okhttp3.Request;

public class Application extends android.app.Application {

    @Override
    public void onCreate() {
        super.onCreate();


        //okHttpUtils初始化
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(300000L, TimeUnit.MILLISECONDS)
                .readTimeout(300000L, TimeUnit.MILLISECONDS)//默认10秒钟 改为30秒
                .addInterceptor(chain -> {
                    Request originalRequest = chain.request();
                    Request newRequest = originalRequest.newBuilder()
                            .build();
                    return chain.proceed(newRequest);
                })
                .addInterceptor(new LoggerInterceptor("fawer.erp", true))
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    private static ProgressDialog progressDialog;//用于显示加载进度的对话框

    public static User loginUser;
    public static boolean isLogout = false;

    //打开ProgressDialog窗口
    public static void showProgressDialog(Context context){
        if(progressDialog!=null)
            dismissProgressDialog();
        progressDialog = new ProgressDialog(context);
        progressDialog.create();
        progressDialog.setMessage("载入中，请稍候...");
        progressDialog.show();
    }

    //打开ProgressDialog窗口
    public static void showProgressDialog(Context context, String msg){
        if(progressDialog!=null)
            dismissProgressDialog();
        progressDialog = new ProgressDialog(context);
        progressDialog.create();
        progressDialog.setMessage(msg);
        progressDialog.show();
    }

    //关闭ProgressDialog窗口
    public static void dismissProgressDialog(){
        if(progressDialog==null)
            return;
        progressDialog.dismiss();
        progressDialog = null;
    }

    //检查有无对应权限
    public static boolean checkPermission(Context context, String permission){
        return ActivityCompat.checkSelfPermission(context, permission)
                == PackageManager.PERMISSION_GRANTED;
    }

    //获取对应权限
    public static void requestPermissions(Activity activity, String[] permissions, int requestCode){
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

    //根据URL从服务器下载文件
    public static void download(String url){
        new Thread(() -> downloadFile(url)).start();
    }

    //设置用户头像大图
    public static void setBigUserImg(Activity activity, ImageView iv){
        Glide.with(activity)
                .load(loginUser.getLocalPath())
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .override(350,350).placeholder(R.drawable.ic_no_img)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(iv);
    }

    //耗时操作，必须使用子线程调用 否则会出现异常
    public static Bitmap getBitmap(Context context, String url, int size) {
        try {
            return Glide.with(context)
                    .asBitmap()
                    .override(size, size)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .load(url).placeholder(R.drawable.ic_no_img)
                    .submit()
                    .get();
        } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); }
        return null;
    }

    //耗时操作，必须使用子线程调用 否则会出现异常
    public static Bitmap getBitmap(Context context, String url) {
        try {
            return Glide.with(context)
                    .asBitmap()
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .load(url).placeholder(R.drawable.ic_no_img)
                    .submit()
                    .get();
        } catch (ExecutionException | InterruptedException e) { e.printStackTrace(); }
        return null;
    }
}
